package com.zhoug.zmui.utils;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.ResourceUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.valuepair.ZMUIAbsKeyValueView;

/**
 * @Author 35574
 * @Date 2020/7/21
 * @Description
 */
public class ZMUIUtils {
    public static final int UN_USED_COLOR = 0;//无效颜色
    public static final int UN_USED_RES_ID= -1;//无效资源id

    /**
     * 设置TextView的文字颜色
     * @param view
     * @param color
     */
    public static void setTextColor(TextView view, int color) {
        if (view != null && color != UN_USED_COLOR) {
            view.setTextColor(color);
        }
    }


    /**
     * 设置TextView的提示文字颜色
     * @param view
     * @param hintColor
     */
    public static void setHintTextColor(TextView view, int hintColor) {
        if (view != null && hintColor != UN_USED_COLOR) {
            view.setHintTextColor(hintColor);
        }
    }

    /**
     *  设置TextView的文字大小
     * @param view
     * @param textSize
     */
    public static void setTextSize(TextView view, float textSize) {
        if (view != null && textSize > 0) {
            view.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        }
    }

    /**
     * dialog的宽度设置为match_parent时且圆角时设置宽度,在屏幕2端流程空隙
     * @return
     */
    public static int getMatchParentMaxWidth(){
        return AppUtils.getScreenSize().x * 4 / 5;
    }


    /**
     * 给指定的ZMUIAbsKeyValueView 添加是否显示必填项的红星
     * @param view
     * @param add
     */
    public static void addRedStar(View view, boolean add) {
        if (view instanceof ZMUIAbsKeyValueView) {
            ((ZMUIAbsKeyValueView) view).setAddRedStar(add);
        } else {
            if (view instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) view;
                int childCount = viewGroup.getChildCount();
                for (int i = 0; i < childCount; i++) {
                    addRedStar(viewGroup.getChildAt(i), add);
                }
            }
        }
    }

    public static int getDp1(Context context){
        if(context!=null){
            return context.getResources().getDimensionPixelOffset(R.dimen.zmui_dp_1);
        }
        return ResourceUtils.dpTopx(1);
    }

    /**
     * 对齐
     *
     * @param name
     * @return
     */
    public static CharSequence getAlignName(CharSequence name,int alignTextLength) {
        if (alignTextLength > 1 && name != null && name.length() < alignTextLength) {
            int nameLength = name.length();
            //需要插入的中文空格数量
            int insetCount = alignTextLength - nameLength;
            //文字间隔
            int nameInterval = nameLength - 1;
            //每个间隔需要插入的空格数
            float space = insetCount * 1.0f / nameInterval;
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < nameLength - 1; i++) {
                builder.append(name.charAt(i));
                if (space > 1) {
                    for (int j = 0; j < space; j++) {
                        builder.append("\u3000");
                    }
                } else if (space == 1f) {
                    builder.append("\u3000");
                } else if (space == 0.5f) {
                    builder.append("\u0020\u0020");
                } else if (space == 0.25f) {
                    builder.append("\u0020");
                }
            }
            builder.append(name.charAt(nameLength - 1));
            return builder.toString();
        }
        return name;
    }

}
